var testMesh, testShape, uvGenerator, tex;

function setupShape(n, r) {
    // Make shape
    var sh = new THREE.Shape();
    for (var i = 0; i < n;i++) {
        var method = i ? 'lineTo' : 'moveTo';
        var a = (i/n) * Math.PI * 2;
        var x = Math.cos(a) * r;
        var y = Math.sin(a) * r;
        sh[method](x, y);
    }

    return sh;
}

function setupTexture(dat) {
    var img = new Image();
    var t = new THREE.Texture(img);
    t.wrapS = THREE.RepeatWrapping;

    img.onload = function() {
        t.needsUpdate = true;
        render();
    };
    img.src = dat;
    return t;
}

function setupMesh(geom, side_texture) {

    /*
    var materials = [
        new THREE.MeshBasicMaterial( { map: side_texture } ),
        new THREE.MeshBasicMaterial( { color: 0x447766 } )
    ]; */

    var legMaterial = new THREE.MeshBasicMaterial( { color: new THREE.Color( 0xeebb99 ) } );

    var mesh = new THREE.Mesh( geom, legMaterial );

    //scene.add(mesh);
    return mesh;
}

function renewMesh() {
    //if (testMesh) {
        //scene.remove(testMesh);
    //}


    testMesh = setupMesh( setupGeometry(testShape) );

    //testMesh.position.set(0, 0, -30);
}

function setupGeometry(shape) {
    // Make extruded geometory
    var exoption = {
        bevelEnabled: true,
        bevelSize: 1,
        amount: 3,
        extrudeMaterial: 0,
        material: 1,
        uvGenerator: uvGenerator
    };

    var geom = shape.extrude(exoption);
    return geom;
}



function makeLeg() {

    uvGenerator = new THREE.UVsUtils.CylinderUVGenerator();
    testShape = setupShape(4, 2);
    holeShape = setupShape(4, 1);

    testShape.holes.push(holeShape);

    //tex = setupTexture( testTextureData );
    renewMesh();

    testMesh.rotation.set( THREE.Math.degToRad( 90 ), 0, 0 );  

    return testMesh;
}
